import {useEffect, useState} from "react";
import {matchPath, useLocation} from "react-router";

import {config, MatchConfig} from "./config";

export default function useSetTitleByLocation() {
  const [title, setTitle] = useState("");
  const [isBack, setIsBack] = useState(false);
  const location = useLocation();

  useEffect(() => {
    const matched: MatchConfig | undefined = config.find(routeItem => matchPath(routeItem.path, location.pathname));
    console.log("当前路由变化:", location);
    setTitle(matched?.title || "");
    setIsBack(Boolean(matched?.isBack));
    // 你可以在这里做日志、埋点、权限检查等
  }, [location]);
  return {
    title,
    isBack
  };
}
